<template>
    <n-spin :show="loading">
        <n-space vertical :size="12">
            <n-space justify="space-between">
                <n-space></n-space>
                <n-space>
                    <n-form inline :model="param" :show-label="false" :show-feedback="false" ref="formRef">
                        <n-form-item>
                            <n-input v-model:value="param.query.pid" style="width: 200px" clearable placeholder="支付单号" />
                        </n-form-item>
                        <n-form-item>
                            <n-input v-model:value="param.query.oid" style="width: 200px" clearable placeholder="交易单ID" />
                        </n-form-item>
                        <n-form-item>
                            <n-tooltip trigger="hover">
                                <template #trigger>
                                    <n-date-picker v-model:value="dates" style="width: 250px" type="daterange" :update-value-on-close="true" />
                                </template>
                                交易时间段
                            </n-tooltip>
                        </n-form-item>
                    </n-form>

                    <n-button @click="doQuery">
                        <template #icon>
                            <n-icon :component="YoutubeSearchedForRound" />
                        </template>
                        查询
                    </n-button>
                </n-space>
            </n-space>
            <n-data-table
                striped
                size="large"
                type="selection"
                :checked-row-keys="srows"
                :max-height="height"
                :min-height="items.length > 0 ? height : 0"
                :row-key="(row:any) => row.id"
                @update:checked-row-keys="onCheckRow"
                :columns="columns"
                :data="items"
            />
            <n-space justify="end">
                <n-pagination show-size-picker :page="param.pager.page" :page-size="param.pager.size" :page-count="pager.all" :page-sizes="[10, 25, 50, 100]" @update:page="onChangePage" @update:page-size="onChangeSize">
                    <template #prefix="{}">当前 {{ pager.now }} / {{ pager.all }} 页，共 {{ pager.row }} 条数据</template>
                </n-pagination>
            </n-space>
        </n-space>
        <Refund :item="data" v-model:show="refundActive" v-if="refundActive" @refresh="execLoad"></Refund>
        <View :item="data" v-model:show="viewActive" v-if="viewActive"></View>
    </n-spin>
</template>

<script lang="ts" setup>
import { h, ref, computed, onActivated, watch } from 'vue'
import { NSpace, NText, NIcon, NButton, NTag } from 'naive-ui'
import { YoutubeSearchedForRound, InfoOutlined, EditNoteOutlined } from '@vicons/material'

import { useStore } from '@/store'
import { Http, Filters } from '@/libs'

import View from './View.vue'
import Refund from './Refund.vue'

import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn' // 导入本地化语言
import customParseFormat from 'dayjs/plugin/customParseFormat'
dayjs.locale('zh-cn')
dayjs.extend(customParseFormat)

const store = useStore()

const loading = computed(() => store.state.loading.flag)

const height = computed(() => store.state.browser.innerHeight - 310)

const columns = [
    {
        type: 'selection',
        fixed: 'left',
        align: 'center',
    },
    {
        title: '支付单号',
        key: 'id',
        width: 180,
        align: 'left',
        ellipsis: {
            tooltip: true,
        },
        render(row: any) {
            let comps: any[] = []
            comps[comps.length] = h(NText, { code: true }, { default: () => row.id })
            return h(NSpace, { wrap: false }, { default: () => comps })
        },
    },
    {
        title: '支付说明',
        key: 'memo',
        align: 'left',
        ellipsis: {
            tooltip: true,
        },
        render(row: any) {
            let comps: any[] = []

            comps[comps.length] = h(NText, { code: true }, { default: () => row.memo })

            return h(NSpace, { wrap: false }, { default: () => comps })
        },
    },
    {
        title: '支付状态',
        key: 'state',
        align: 'left',
        width: 200,
        ellipsis: {
            tooltip: true,
        },
        render(row: any) {
            let comps: any[] = []

            if (row.state == 1) {
                comps[comps.length] = h(NTag, { size: 'small', type: 'primary' }, { default: () => '支付中' })
            } else if (row.state == 2) {
                comps[comps.length] = h(NTag, { size: 'small', type: 'info' }, { default: () => '已完成' })
            } else if (row.state == 3) {
                comps[comps.length] = h(NTag, { size: 'small', type: 'error' }, { default: () => '未支付' })
            } else if (row.state == 9) {
                comps[comps.length] = h(NTag, { size: 'small', type: 'warning' }, { default: () => '已取消' })
            }

            if (row.cnl == 20) {
                comps[comps.length] = h(NTag, { round: true, bordered: false, size: 'small', type: 'primary' }, { default: () => '微信扫码支付' })
            } else if (row.cnl == 21) {
                comps[comps.length] = h(NTag, { round: true, bordered: false, size: 'small', type: 'primary' }, { default: () => '微信小程序支付' })
            } else if (row.cnl == 22) {
                comps[comps.length] = h(NTag, { round: true, bordered: false, size: 'small', type: 'primary' }, { default: () => '微信App支付' })
            } else if (row.cnl == 23) {
                comps[comps.length] = h(NTag, { round: true, bordered: false, size: 'small', type: 'primary' }, { default: () => '微信信用分' })
            } else if (row.cnl == 30) {
                comps[comps.length] = h(NTag, { round: true, bordered: false, size: 'small', type: 'info' }, { default: () => '支付宝扫码支付' })
            } else if (row.cnl == 31) {
                comps[comps.length] = h(NTag, { round: true, bordered: false, size: 'small', type: 'info' }, { default: () => '支付宝小程序支付' })
            } else if (row.cnl == 32) {
                comps[comps.length] = h(NTag, { round: true, bordered: false, size: 'small', type: 'info' }, { default: () => '支付宝App支付' })
            } else if (row.cnl == 33) {
                comps[comps.length] = h(NTag, { round: true, bordered: false, size: 'small', type: 'info' }, { default: () => '芝麻信用分' })
            }

            return h(NSpace, { justify: 'space-between', style: { width: '100%' }, wrap: false }, { default: () => comps })
        },
    },
    {
        title: '支付金额',
        key: 'value',
        align: 'right',
        width: 200,
        ellipsis: {
            tooltip: true,
        },
        render(row: any) {
            let comps: any[] = []

            comps[comps.length] = h('span', {}, { default: () => Filters.money(row.value) + ' 元' })

            return h(NSpace, { wrap: false }, { default: () => comps })
        },
    },
    {
        title: '退款金额',
        key: 'refund',
        align: 'right',
        width: 200,
        ellipsis: {
            tooltip: true,
        },
        render(row: any) {
            let comps: any[] = []
            comps[comps.length] = h('span', {}, { default: () => Filters.money(row.refund.value) + ' 元' })

            return h(NSpace, { wrap: false }, { default: () => comps })
        },
    },
    {
        title: '发起时间',
        key: 'value',
        align: 'left',
        width: 200,
        ellipsis: {
            tooltip: true,
        },
        render(row: any) {
            let comps: any[] = []
            comps[comps.length] = h('span', {}, { default: () => row.create })
            return h(NSpace, { wrap: false }, { default: () => comps })
        },
    },
    {
        title: '完成/关闭时间',
        key: 'value',
        align: 'left',
        width: 200,
        ellipsis: {
            tooltip: true,
        },
        render(row: any) {
            let comps: any[] = []
            comps[comps.length] = h('span', {}, { default: () => row.finish || row.close })
            return h(NSpace, { wrap: false }, { default: () => comps })
        },
    },
    {
        title: '操作',
        key: 'actions',
        width: 200,
        align: 'left',
        fixed: 'right',
        render(row: any) {
            return h(
                NSpace,
                { justify: 'start' },
                {
                    default: () => [
                        h(
                            NButton,
                            {
                                size: 'small',
                                onClick: () => {
                                    srows.value = []
                                    data.value = row
                                    viewActive.value = true
                                },
                            },
                            { default: () => '详情', icon: () => h(NIcon, {}, { default: () => h(InfoOutlined, {}, {}) }) }
                        ),
                        h(
                            NButton,
                            {
                                size: 'small',
                                disabled: row.refund.flag || row.state != 2,
                                onClick: () => {
                                    srows.value = []
                                    data.value = row
                                    refundActive.value = true
                                },
                            },
                            { default: () => '退款', icon: () => h(NIcon, {}, { default: () => h(EditNoteOutlined, {}, {}) }) }
                        ),
                    ],
                }
            )
        },
    },
]

const srows = ref<string[]>([])

const onCheckRow = (keys: string[]) => {
    srows.value = keys
}

const onChangeSize = (size: number) => {
    param.value.pager.page = 1
    param.value.pager.size = size
    execLoad()
}

const onChangePage = (page: number) => {
    param.value.pager.page = page
    execLoad()
}

const dates = ref<number[]>([dayjs().subtract(1, 'month').valueOf(), dayjs().valueOf()])
watch([dates, () => dates.value], (nv, _ov) => {
    let v = nv[0] as number[]
    param.value.query.begin = dayjs(v[0]).format('YYYY-MM-DD')
    param.value.query.close = dayjs(v[1]).format('YYYY-MM-DD')
})

const param = ref({
    query: {
        begin: dayjs().subtract(1, 'month').format('YYYY-MM-DD'),
        close: dayjs().format('YYYY-MM-DD'),
        cnl: 0,
        state: 0,
        pid: '',
        oid: '',
    },
    pager: {
        page: 1,
        size: 10,
        time: new Date().getTime(),
    },
})

const pager = ref({
    now: 1,
    row: 0,
    all: 0,
    fms: 0,
})
const items = ref([])

const doQuery = () => {
    param.value.pager.page = 1
    execLoad()
}

const execLoad = () => {
    Http.post('/ops/fin/online/payment/list', param.value).then(resp => {
        if (resp.stat.err > 0) {
            return
        }
        if (resp.data.pager.now == 1) {
            param.value.pager.time = resp.data.pager.fms
        }
        srows.value = []
        pager.value = resp.data.pager
        items.value = resp.data.items
    })
}

const data = ref<any>({})

const refundActive = ref(false)
const viewActive = ref(false)

onActivated(() => {
    execLoad()
})
</script>
